package com.postgresql.geotools.main;

import com.postgresql.geotools.GeoTable;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.jdbc.JDBCDataStore;
import org.opengis.feature.simple.SimpleFeatureType;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;

/**
 * @author: 顾志杰
 */
public class GeoToolsImportText {


    public static GeoTable connnection2mysql() {

        return new GeoTable();
    }

    public static JDBCDataStore createTable(GeoTable ds, SimpleFeatureSource featureSource) {
        SimpleFeatureType schema = featureSource.getSchema();
        try {
            //创建数据表
            ds.createSchema(schema);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return ds.connnection2mysql();
    }

    public static SimpleFeatureSource readSHP() {
        String shpfile = "C:/Users/顾志杰/Desktop/grid011.shp";
        SimpleFeatureSource featureSource = null;
        try {
            File file = new File(shpfile);
            ShapefileDataStore shpDataStore = null;

            shpDataStore = new ShapefileDataStore(file.toURL());
            //设置编码
            Charset charset = Charset.forName("GBK");
            shpDataStore.setCharset(charset);
            String tableName = shpDataStore.getTypeNames()[0];
            featureSource = shpDataStore.getFeatureSource(tableName);
            System.out.println("读取成功");
        } catch (Exception e) {
            System.out.println("读取失败");
            e.printStackTrace();
        }
        return featureSource;
    }


    public static void main(String[] args) {
//        connnection2mysql();
        SimpleFeatureSource featureSource  = readSHP();
        SimpleFeatureType schema = featureSource.getSchema();
        JDBCDataStore table = createTable(connnection2mysql(), featureSource);
        String s = schema.getTypeName().toLowerCase();
        System.out.println(s);
        int i=0;

    }
}
